<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="black"><meta name="author" content="李子康"><meta name="copyright" content="李子康"><meta name="generator" content="Hexo 4.2.1"><meta name="theme" content="hexo-theme-yun"><title>Unity连接Mysql数据库，小项目学习 | Lizikang_Blog</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="none" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.11/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_stqaphw3j4.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><script src="https://cdn.jsdelivr.net/npm/pjax@latest/pjax.min.js" defer></script><script src="/js/pjax.js" defer></script><link rel="shortcut icon" type="image/svg+xml" href="/images/%E7%9A%AE%E5%8D%A1%E4%B8%98-2.ico"><link rel="mask-icon" href="/images/%E7%9A%AE%E5%8D%A1%E4%B8%98-2.ico" color="black"><link rel="alternate icon" href="/yun.ico"><link rel="preload" href="/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/js/utils.js" as="script"><link rel="preload" href="/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><link rel="stylesheet" href="/css/hexo-theme-yun.css"><link rel="alternate" href="/atom.xml" title="Lizikang_Blog"><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"root":"/","title":["Li","Zi","Kang","Blog"],"version":"0.9.2","anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><script src="//at.alicdn.com/t/font_1929835_1z6thct9lfe.js" async></script><meta name="description" content="https:&#x2F;&#x2F;editor.csdn.net&#x2F;md&#x2F;?articleId&#x3D;117034983  制作中可能用到的资料： https:&#x2F;&#x2F;dev.mysql.com&#x2F;downloads&#x2F; 视频教学 https:&#x2F;&#x2F;www.bilibili.com&#x2F;video&#x2F;BV1S7411M7P7?from&#x3D;search&amp;seid&#x3D;1780740931854569426 MySQL Connector&#x2F;">
<meta property="og:type" content="article">
<meta property="og:title" content="Unity连接Mysql数据库，小项目学习">
<meta property="og:url" content="http://yoursite.com/2021/05/19/Unity/Unity%E8%BF%9E%E6%8E%A5Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%EF%BC%8C%E5%B0%8F%E9%A1%B9%E7%9B%AE%E5%AD%A6%E4%B9%A0/index.html">
<meta property="og:site_name" content="Lizikang_Blog">
<meta property="og:description" content="https:&#x2F;&#x2F;editor.csdn.net&#x2F;md&#x2F;?articleId&#x3D;117034983  制作中可能用到的资料： https:&#x2F;&#x2F;dev.mysql.com&#x2F;downloads&#x2F; 视频教学 https:&#x2F;&#x2F;www.bilibili.com&#x2F;video&#x2F;BV1S7411M7P7?from&#x3D;search&amp;seid&#x3D;1780740931854569426 MySQL Connector&#x2F;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210510215752787.png">
<meta property="og:image" content="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519155346643.png">
<meta property="og:image" content="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519155426975.png">
<meta property="og:image" content="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519162952096.png">
<meta property="og:image" content="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519163023360.png">
<meta property="og:image" content="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519163249232.png">
<meta property="og:image" content="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519163314720.png">
<meta property="og:image" content="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519163334533.png">
<meta property="article:published_time" content="2021-05-19T08:23:52.000Z">
<meta property="article:modified_time" content="2021-05-24T06:45:45.782Z">
<meta property="article:author" content="李子康">
<meta property="article:tag" content="Unity">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210510215752787.png"><script src="/js/ui/mode.js"></script><link rel="alternate" href="/atom.xml" title="Lizikang_Blog" type="application/atom+xml">
</head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script defer src="/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script defer src="/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="文章目录"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="站点概览"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/about/" title="李子康"><img width="96" loading="lazy" src="/images/bak.jpg" alt="李子康"></a><div class="site-author-name"><a href="/about/">李子康</a></div><a class="site-name" href="/about/site.html">Lizikang_Blog</a><sub class="site-subtitle"></sub><div class="site-desciption">个人技术博客</div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/" title="我的主页"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/archives/" title="归档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">27</span></a></div><div class="site-state-item"><a href="/categories/" title="分类"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">13</span></a></div><div class="site-state-item"><a href="/tags/" title="标签"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">9</span></a></div><a class="site-state-item hty-icon-button" href="https://yun.yunyoujun.cn" target="_blank" rel="noopener" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://wpa.qq.com/msgrd?v=3&amp;uin=1191787635&amp;site=qq&amp;menu=yes" title="QQ" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://github.com/lizikanglzk" title="GitHub" target="_blank" style="color:#181717"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="mailto:1191787635@qq.com" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://gitee.com/li_zikang" title="gitee" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-gitee-copy"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://music.163.com/#/user/home?id=506556053" title="网易云音乐" target="_blank" style="color:#C10D0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://blog.csdn.net/qq_43845915" title="csdn" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-csdn11"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://www.zhihu.com/people/zou-guo-lu-guo-bu-yao-cuo-guo-88" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://space.bilibili.com/352594163" title="哔哩哔哩动画" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微信" target="_blank" style="color:#1AAD19"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-2-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="/atom.xml" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="/game1" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-youxi-copy-copy"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://travellings.now.sh/" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-send-plane-2-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-men-line"></use></svg></a><a class="links-item hty-icon-button" href="/girls/" title="我的老婆们" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-women-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#mysql服务启动"><span class="toc-text">mysql服务启动</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#数据库设计"><span class="toc-text">数据库设计</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#unity-GUI-界面设计："><span class="toc-text">unity GUI 界面设计：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#写好unity-连接mysql的包装类："><span class="toc-text">写好unity 连接mysql的包装类：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#登录按钮绑定脚本User-Log-In-js"><span class="toc-text">登录按钮绑定脚本User Log In.js</span></a></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/05/19/Unity/Unity%E8%BF%9E%E6%8E%A5Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%EF%BC%8C%E5%B0%8F%E9%A1%B9%E7%9B%AE%E5%AD%A6%E4%B9%A0/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="李子康"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="Lizikang_Blog"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">Unity连接Mysql数据库，小项目学习</h1><div class="post-meta"><div class="post-time" style="display:inline-block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="创建时间：2021-05-19 16:23:52" itemprop="dateCreated datePublished" datetime="2021-05-19T16:23:52+08:00">2021-05-19</time><span class="post-meta-divider">-</span><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-2-line"></use></svg></span> <time title="修改时间：2021-05-24 14:45:45" itemprop="dateModified" datetime="2021-05-24T14:45:45+08:00">2021-05-24</time></div><span class="post-busuanzi"><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读次数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg> <span id="busuanzi_value_page_pv"></span></span></span><div class="post-classify"><span class="post-category"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-line"></use></svg></span> <span itemprop="about" itemscope itemtype="https://schema.org/Thing"><a class="category" href="/categories/Unity/" style="--text-color:var(--hty-text-color)" itemprop="url" rel="index"><span itemprop="text">Unity</span></a></span></span><span class="post-tag"><span class="post-meta-divider">-</span><a class="tag" href="/tags/Unity/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">Unity</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:black;"><p><a href="https://editor.csdn.net/md/?articleId=117034983" target="_blank" rel="noopener">https://editor.csdn.net/md/?articleId=117034983</a></p>
<blockquote>
<p><strong>制作中可能用到的资料：</strong></p>
<p><a href="https://dev.mysql.com/downloads/" target="_blank" rel="noopener">https://dev.mysql.com/downloads/</a></p>
<p>视频教学</p>
<p><a href="https://www.bilibili.com/video/BV1S7411M7P7?from=search&amp;seid=1780740931854569426" target="_blank" rel="noopener">https://www.bilibili.com/video/BV1S7411M7P7?from=search&amp;seid=1780740931854569426</a></p>
<p>MySQL Connector/NET的下载以及安装教程</p>
<p><a href="https://blog.csdn.net/weixin_42220953/article/details/108341921" target="_blank" rel="noopener">https://blog.csdn.net/weixin_42220953/article/details/108341921</a></p>
<p>下载地址：</p>
<p><a href="https://downloads.mysql.com/archives/c-net/" target="_blank" rel="noopener">https://downloads.mysql.com/archives/c-net/</a></p>
<p>Mysql 8.0.11 出现1251- Client does not support authentication protocol 错误解决办法</p>
<p><a href="https://blog.csdn.net/qq_41030861/article/details/100012275" target="_blank" rel="noopener">https://blog.csdn.net/qq_41030861/article/details/100012275</a></p>
<p><a href="https://blog.csdn.net/qq_34735841/article/details/100896095" target="_blank" rel="noopener">https://blog.csdn.net/qq_34735841/article/details/100896095</a></p>
<p><img src="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210510215752787.png" alt="image-20210510215752787" loading="lazy"></p>
<p><a href="https://www.bilibili.com/read/cv4790659" target="_blank" rel="noopener">https://www.bilibili.com/read/cv4790659</a></p>
</blockquote>
<h4 id="mysql服务启动"><a href="#mysql服务启动" class="headerlink" title="mysql服务启动"></a>mysql服务启动</h4><p><img src="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519155346643.png" alt="image-20210519155346643" loading="lazy"></p>
<h4 id="数据库设计"><a href="#数据库设计" class="headerlink" title="数据库设计"></a>数据库设计</h4><p><img src="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519155426975.png" alt="image-20210519155426975" loading="lazy"></p>
<h4 id="unity-GUI-界面设计："><a href="#unity-GUI-界面设计：" class="headerlink" title="unity GUI 界面设计："></a>unity GUI 界面设计：</h4><p><img src="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519162952096.png" alt="image-20210519162952096" loading="lazy"></p>
<h4 id="写好unity-连接mysql的包装类："><a href="#写好unity-连接mysql的包装类：" class="headerlink" title="写好unity 连接mysql的包装类："></a>写好unity 连接mysql的包装类：</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br></pre></td><td class="code"><pre><code class="hljs c#">using System;<br>using System.Data;<br>using MySql.Data.MySqlClient;<br>using System.Diagnostics;<br>using UnityEngine;<br><br>public class MySqlAccess &#123;<br><br>    private static MySqlConnection mySqlConnection;&#x2F;&#x2F;连接类对象<br>    private static string host;&#x2F;&#x2F;IP地址<br>    private static string port;&#x2F;&#x2F;端口号<br>    private static string userName;&#x2F;&#x2F;用户名<br>    private static string password;&#x2F;&#x2F;密码<br>    private static string databaseName;&#x2F;&#x2F;数据库名称<br><br>    enum Operate &#123;<br>        Select, Insert<br>    &#125;<br>    &#x2F;&#x2F;&#x2F; &lt;summary&gt;<br>    &#x2F;&#x2F;&#x2F; 构造方法<br>    &#x2F;&#x2F;&#x2F; &lt;&#x2F;summary&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;param name&#x3D;&quot;_host&quot;&gt;ip地址&lt;&#x2F;param&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;param name&#x3D;&quot;_userName&quot;&gt;用户名&lt;&#x2F;param&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;param name&#x3D;&quot;_password&quot;&gt;密码&lt;&#x2F;param&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;param name&#x3D;&quot;_databaseName&quot;&gt;数据库名称&lt;&#x2F;param&gt;<br>    public MySqlAccess(string _host, string _port, string _userName, string _password, string _databaseName) &#123;<br>        host &#x3D; _host;<br>        port &#x3D; _port;<br>        userName &#x3D; _userName;<br>        password &#x3D; _password;<br>        databaseName &#x3D; _databaseName;<br>        OpenSql();<br>    &#125;<br>    &#x2F;&#x2F; 打开数据库<br>    public void OpenSql() &#123;<br>        try &#123;<br>            string mySqlString &#x3D;<br>                &quot;Database&#x3D;&quot; + databaseName + &quot;;DataSource&#x3D;&quot; + host + &quot;;User&#x3D;&quot; + userName + &quot;;Password&#x3D;&quot; + password + &quot;;port&#x3D;&quot; + port + &quot;;&quot;;<br>            mySqlConnection &#x3D; new MySqlConnection(mySqlString);<br>            UnityEngine.Debug.Log(&quot;连接成功&quot;);<br>            &#x2F;&#x2F;if(mySqlConnection.State &#x3D;&#x3D; ConnectionState.Closed)<br>            mySqlConnection.Open();<br><br>        &#125;<br>        catch (Exception e) &#123;<br>            throw new Exception(&quot;服务器连接失败，请重新检查MySql服务是否打开。&quot; + e.Message.ToString());<br>        &#125;<br><br>    &#125;<br>    &#x2F;&#x2F; 关闭数据库<br>    public void CloseSql() &#123;<br>        if (mySqlConnection !&#x3D; null) &#123;<br>            mySqlConnection.Close();<br>            mySqlConnection.Dispose();<br>            mySqlConnection &#x3D; null;<br>        &#125;<br>    &#125;<br><br>    &#x2F;&#x2F;&#x2F; &lt;summary&gt;<br>    &#x2F;&#x2F;&#x2F; 查询数据<br>    &#x2F;&#x2F;&#x2F; &lt;&#x2F;summary&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;param name&#x3D;&quot;tableName&quot;&gt;表名&lt;&#x2F;param&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;param name&#x3D;&quot;items&quot;&gt;要查询的列&lt;&#x2F;param&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;param name&#x3D;&quot;whereColumnName&quot;&gt;查询的条件列&lt;&#x2F;param&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;param name&#x3D;&quot;operation&quot;&gt;条件操作符&lt;&#x2F;param&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;param name&#x3D;&quot;value&quot;&gt;条件的值&lt;&#x2F;param&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;returns&gt;&lt;&#x2F;returns&gt;<br>    public DataSet Select(string tableName, string[] items, string[] whereColumnName, string[] operation, string[] value) &#123;<br>        if (whereColumnName.Length !&#x3D; operation.Length || operation.Length !&#x3D; value.Length) &#123;<br>            throw new Exception(&quot;输入不正确：&quot; + &quot;要查询的条件、条件操作符、条件值 的数量不一致！&quot;);<br>        &#125;<br>        string query &#x3D; &quot;Select &quot; + items[0];<br>        for (int i &#x3D; 1; i &lt; items.Length; i++) &#123;<br>            query +&#x3D; &quot;,&quot; + items[i];<br>        &#125;<br><br>        query +&#x3D; &quot; FROM &quot; + tableName + &quot; WHERE &quot; + whereColumnName[0] + &quot; &quot; + operation[0] + &quot; &#39;&quot; + value[0] + &quot;&#39;&quot;;<br>        for (int i &#x3D; 1; i &lt; whereColumnName.Length; i++) &#123;<br>            query +&#x3D; &quot; and &quot; + whereColumnName[i] + &quot; &quot; + operation[i] + &quot; &#39;&quot; + value[i] + &quot;&#39;&quot;;<br>        &#125;<br>        return QuerySet(query, Operate.Select);<br><br>    &#125;<br><br>    &#x2F;&#x2F;&#x2F; &lt;summary&gt;<br>    &#x2F;&#x2F;&#x2F; 执行SQL语句<br>    &#x2F;&#x2F;&#x2F; &lt;&#x2F;summary&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;param name&#x3D;&quot;sqlString&quot;&gt;sql语句&lt;&#x2F;param&gt;<br>    &#x2F;&#x2F;&#x2F; &lt;returns&gt;&lt;&#x2F;returns&gt;<br>    private DataSet QuerySet(string sqlString, Operate op) &#123;<br>        <br>        if (mySqlConnection.State &#x3D;&#x3D; ConnectionState.Open) &#123;<br>            DataSet ds &#x3D; new DataSet();<br>            MySqlDataAdapter mySqlAdapter &#x3D; new MySqlDataAdapter(sqlString, mySqlConnection);<br>            mySqlAdapter.Fill(ds);<br>            switch (op) &#123;<br>                case Operate.Select: return ds.Tables[0].Rows.Count &gt; 0 ? ds : null;<br>                case Operate.Insert:return null;<br>            &#125;<br>            mySqlAdapter.Dispose();<br>        &#125;<br>        return null;<br>    &#125;<br><br><br>    public bool Insert(string tableName, string[] items, string[] value) &#123;<br>        DataSet ds &#x3D; QuerySet(&quot;Select level from test where name&#x3D;&#39;&quot; + value[0] + &quot;&#39;&quot;, Operate.Select);<br>        if (ds !&#x3D; null) &#123;<br>            return false;<br>        &#125;<br>        else &#123;<br>            if (items.Length !&#x3D; value.Length) &#123;<br>                throw new Exception(&quot;输入不正确：&quot; + &quot;要查询的条件、条件操作符、条件值 的数量不一致！&quot;);<br>            &#125;<br>            string query &#x3D; &quot;Insert Into &quot; + tableName + &quot; ( &quot; + items[0];<br>            for (int i &#x3D; 1; i &lt; items.Length; i++) &#123;<br>                query +&#x3D; &quot;,&quot; + items[i];<br>            &#125;<br>            query +&#x3D; &quot; ) Values ( &#39;&quot; + value[0] + &quot;&#39;&quot;;<br>            for (int i &#x3D; 1; i &lt; value.Length; i++) &#123;<br>                query +&#x3D; &quot;,&#39;&quot; + value[i] + &quot;&#39;&quot;;<br>            &#125;<br>            query +&#x3D; &quot; )&quot;;<br>            UnityEngine.Debug.Log(query);<br>            QuerySet(query, Operate.Insert);<br>        &#125;<br>        return true;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="登录按钮绑定脚本User-Log-In-js"><a href="#登录按钮绑定脚本User-Log-In-js" class="headerlink" title="登录按钮绑定脚本User Log In.js"></a>登录按钮绑定脚本User Log In.js</h4><p>当点击登录后调用脚本中OnLogInBtnClick()方法：</p>
<p><img src="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519163023360.png" alt="image-20210519163023360" loading="lazy"></p>
<p>UserLogIn.js：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><code class="hljs C#">using System.Collections;<br>using System.Collections.Generic;<br>using System.Data;<br>using UnityEngine;<br>using UnityEngine.UI;<br><br>public class UserLogIn : MonoBehaviour<br>&#123;<br>    public InputField userNameInput;<br>    public InputField passwordInput;<br>    public Image mes;<br>    public string host;&#x2F;&#x2F;IP地址<br>    public string port;&#x2F;&#x2F;端口号<br>    public string userName;&#x2F;&#x2F;用户名<br>    public string password;&#x2F;&#x2F;密码<br>    public string databaseName;&#x2F;&#x2F;数据库名称<br>    protected MySqlAccess mysql;&#x2F;&#x2F;封装好的数据库类<br>    &#x2F;&#x2F; Start is called before the first frame update<br>    void Start()<br>    &#123;<br>        mysql &#x3D; new MySqlAccess(host, port, userName, password, databaseName);<br>    &#125;<br><br>    &#x2F;&#x2F; Update is called once per frame<br>    void Update()<br>    &#123;<br>        <br>    &#125;<br><br>    public void OnLogInBtnClick() &#123;<br>        mysql.OpenSql();<br>        &#x2F;&#x2F;获取数据<br>        DataSet ds &#x3D; mysql.Select(&quot;test&quot;, new string[] &#123; &quot;password&quot;,&quot;level&quot; &#125;, new string[] &#123; &quot;name&quot;&#125;, new string[] &#123; &quot;&#x3D;&quot;&#125;, new string[] &#123; userNameInput.text&#125;);<br>        if (ds !&#x3D; null) &#123;<br>            DataTable table &#x3D; ds.Tables[0];<br>            List&lt;string&gt;data&#x3D;new List&lt;string&gt;();<br>            foreach (DataRow row in table.Rows) &#123;<br>                foreach (DataColumn col in table.Columns) &#123;<br>                    data.Add(row[col].ToString());<br>                &#125;<br>            &#125;<br>            if (data[0]&#x3D;&#x3D;passwordInput.text) &#123;<br>                Debug.Log(&quot;登陆成功&quot;);<br>                mes.color &#x3D; Color.green;<br>            &#125;<br>            else &#123;<br>                Debug.Log(&quot;登陆失败&quot;);<br>                mes.color &#x3D; Color.red;<br>            &#125;<br>        &#125;<br>        else &#123;<br>            Debug.Log(&quot;登陆失败&quot;);<br>            mes.color &#x3D; Color.red;<br>        &#125;<br>        mysql.CloseSql();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>启动后：</p>
<p>初始状态：</p>
<p><img src="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519163249232.png" alt="image-20210519163249232" loading="lazy"></p>
<p>登录成功：</p>
<p><img src="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519163314720.png" alt="image-20210519163314720" loading="lazy"></p>
<p>登录失败：</p>
<p><img src="https://gitee.com/li_zikang/lzk-image/raw/master/img/image-20210519163334533.png" alt="image-20210519163334533" loading="lazy"></p>
<blockquote>
<p>扩展：注册功能，有时间写</p>
</blockquote>
</div></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/2021/05/21/%E4%BF%A1%E6%81%AF%E5%AE%89%E5%85%A8/%E4%BF%A1%E6%81%AF%E5%AE%89%E5%85%A8%E5%AE%9E%E9%AA%8C%E4%B8%89/" rel="prev" title="信息安全实验三、网络监听"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">信息安全实验三、网络监听</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/2021/05/18/%E4%BF%A1%E6%81%AF%E5%AE%89%E5%85%A8/%E4%BF%A1%E6%81%AF%E5%AE%89%E5%85%A8%E5%AE%9E%E9%AA%8C%E4%BA%8C/" rel="next" title="信息安全实验二、漏洞扫描与病毒防治"><span class="post-nav-text">信息安全实验二、漏洞扫描与病毒防治</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div><div id="comment"><div class="comment-tooltip text-center"></div><div id="valine-container"></div><script src="https://cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script><script>function initValine() {
  const valineConfig = {"enable":true,"appId":"q22e7srtjevOqYmzusWCYIru-gzGzoHsz","appKey":"PMyzTxHUFQ77nPj1IWMcMDK3","placeholder":"畅所欲言！","avatar":null,"meta":["nick","mail","link"],"pageSize":10,"visitor":false,"highlight":true,"recordIP":false,"enableQQ":true,"el":"#valine-container","lang":"zh-cn"}
  valineConfig.path = window.location.pathname
  new Valine(valineConfig)
}
setTimeout(initValine, 1000)</script></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2020 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> 李子康</span></div><div class="powered"><span>由 <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> 驱动 v4.2.1</span><span class="footer-separator">|</span><span>主题 - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v0.9.2</span></div><div class="live_time"><span>本博客已萌萌哒地运行</span><span id="display_live_time"></span><span class="moe-text">(●'◡'●)</span><script>function blog_live_time() {
  window.setTimeout(blog_live_time, 1000);
  const start = new Date('2020-07-05T00:00:00');
  const now = new Date();
  const timeDiff = (now.getTime() - start.getTime());
  const msPerMinute = 60 * 1000;
  const msPerHour = 60 * msPerMinute;
  const msPerDay = 24 * msPerHour;
  const passDay = Math.floor(timeDiff / msPerDay);
  const passHour = Math.floor((timeDiff % msPerDay) / 60 / 60 / 1000);
  const passMinute = Math.floor((timeDiff % msPerHour) / 60 / 1000);
  const passSecond = Math.floor((timeDiff % msPerMinute) / 1000);
  display_live_time.innerHTML = " " + passDay + " 天 " + passHour + " 小时 " + passMinute + " 分 " + passSecond + " 秒";
}
blog_live_time();
</script></div><div id="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_site_uv" title="总访客量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-user-line"></use></svg></span><span id="busuanzi_value_site_uv"></span></span><span class="footer-separator">|</span><span id="busuanzi_container_site_pv" title="总访问量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg></span><span id="busuanzi_value_site_pv"></span></span></div></footer><a class="hty-icon-button" id="goUp" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="black" stroke-width="2" stroke-linecap="round"></circle></svg></a></div><script defer src="/js/utils.js"></script><script defer src="/js/hexo-theme-yun.js"></script><script defer src="/js/player.js"></script></body></html>